﻿using System.Collections.Generic;
using System.Threading;

namespace System.Data.SQL.Linq.Mapping
{
    public class AttributedMetaModel : MetaModel
    {
        private ReaderWriterLock locker;
        private Dictionary<Type, MetaType> types;
        private Dictionary<Type, MetaTable> tables;
        private Dictionary<MetaPosition, MetaFunction> functions;
        private bool initStaticTables;
        private bool initFunctions; 

        public MappingSource MappingSource { get; private set; }
        public Type ContextType { get; private set; }
        public string DatabaseName { get; private set; }
        public Type ProviderType { get; private set; }

        public AttributedMetaModel(MappingSource mappingSource, Type contextType)
        {
            if (mappingSource == null || contextType == null) throw new ArgumentNullException();
            
            MappingSource = mappingSource;
            ContextType = contextType;

            locker = new ReaderWriterLock();
            types = new Dictionary<Type, MetaType>();
            tables = new Dictionary<Type, MetaTable>();
            functions = new Dictionary<MetaPosition, MetaFunction>();

            DatabaseAttribute[] das = (DatabaseAttribute[])contextType.GetCustomAttributes(typeof(DatabaseAttribute), false);
            DatabaseName = (das != null && das.Length > 0) ? das[0].Name : contextType.Name; 
        }

        private void InitStaticTables()
        {
            if (initStaticTables) return;

            locker.AcquireWriterLock(Timeout.Infinite);
            try
            {
            }
            catch
            {

            }
            finally
            {
                locker.ReleaseWriterLock();
            }
        }

        private void InitFunctions()
        {
            if (initFunctions) return;

            locker.AcquireWriterLock(Timeout.Infinite); 
            try
            {
            }
            catch 
            {
            	
            }
            finally
            {
                locker.ReleaseWriterLock();
            }
        }

        public MetaTable GetTable(Type rowType)
        {
            throw new NotImplementedException();
        }

        public MetaType GetMetaType(Type type)
        {
            throw new NotImplementedException();
        }

        public MetaFunction GetFunction(Reflection.MethodInfo method)
        {
            throw new NotImplementedException();
        }

        public IEnumerable<MetaTable> GetTables()
        {
            throw new NotImplementedException();
        }

        public IEnumerable<MetaFunction> GetFunctions()
        {
            throw new NotImplementedException();
        }
    }
}
